package com.tairanchina.md.meeting.rabbit;

import com.tairanchina.md.elife.rabbit.client.consumer.RabbitConsumerClient;
import com.tairanchina.md.elife.rabbit.constants.ConnConstants;
import com.tairanchina.md.elife.rabbit.task.RabbitMQTask;
import com.tairanchina.md.mallorder.order.dto.YearPartyWaterDto;
import com.tairanchina.md.meeting.constants.MeetingConstants;
import com.tairanchina.md.meeting.constants.MeetingProvider;
import com.tairanchina.md.meeting.entity.YearMeetingUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by lxliuxuan on 2017/1/4.
 */

public class SignTask implements Runnable,RabbitMQTask {

    Logger logger = LoggerFactory.getLogger(SignTask.class);
    @Override
    public void run() {
        RabbitConsumerClient consumerClient = ConnConstants.getConsumerClient();


        //处理接收消息
        while (true)
        {
            try {
                consumerClient.receiveMessage(MeetingConstants.QUEUE_NAME_YEAR_WATER_LOCAL,this);
            }catch (Exception e){
                e.printStackTrace();
                logger.error("用户签到消息消费失败,e:{}",e);
            }
        }
    }


    @Override
    public void consumerMsg(Object o, Object... objects) throws Exception {

        YearPartyWaterDto yearMeetingMsg = (YearPartyWaterDto) o;
        if(yearMeetingMsg.getWaterTime() >= MeetingConstants.STARTTIME && yearMeetingMsg.getWaterTime() <= MeetingConstants.ENDTIME){

            //判断用户是否在白名单，如果不在不进行处理
            //判断用户是否签到，如果已签到不进行处理
            YearMeetingUser user = MeetingProvider.userService.findUserByPhone(yearMeetingMsg.getUserPhone());

            if (user != null) {

                // 未签到，将白名单中的用户信息更新为已签到 ，并进行弹幕
                if (user.getIsSign() != 1) {
                    user.setIsSign(1);

                    //更新用户信息
                    MeetingProvider.userService.updateUser(user);

                }
            }
        }
    }
}
